package com.cpi.cornpp.grinddistribution.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.grinddistribution.exception.GrindDistributionException;
import com.cpi.cornpp.grinddistribution.form.GrindDistributionForm;
import com.cpi.cornpp.grinddistribution.service.GrindDistributionService;
import com.cs.common.utils.NumberUtil;

public class GrindDistributionAction extends MappingDispatchAction {
	private static final String DEFAULT_VALUE = "default";//$NON-NLS-1$
	private static final String VERSION_ID = "versionId";//$NON-NLS-1$
	private static final String SUCCESS_UPDATE_LABEL = "common.edit.success";//$NON-NLS-1$
	private static final String MESSAGE = "MESSAGE";//$NON-NLS-1$
	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws GrindDistributionException{
		GrindDistributionService grindDistributionService = null;
		HttpSession httpSession = null;
		int versionId = 0;
		List grindDistributionViews = null;
		GrindDistributionForm grindDistributionForm = null;
		try{
			grindDistributionForm = (GrindDistributionForm)form;
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			grindDistributionService = new GrindDistributionService();
			grindDistributionViews = grindDistributionService.getGrindDistribution(versionId);
			grindDistributionForm.setGrindDistributionViews(grindDistributionViews);
			grindDistributionForm.reset(mapping, request);
		}catch(GrindDistributionException grindDistributionException){
			throw grindDistributionException;
		}catch(Exception exception){
			GrindDistributionException grindDistributionException = null;
			grindDistributionException = new GrindDistributionException(exception, CornPPException.LAYER_ACTION, CornPPException.ACTION_LISTS);
			exception.printStackTrace();
			throw grindDistributionException;
		}
		return mapping.findForward(DEFAULT_VALUE);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws GrindDistributionException{
		GrindDistributionService grindDistributionService = null;
		GrindDistributionForm grindDistributionForm = null;
		List grindDistributionViews = null;
		String messageError = null;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			grindDistributionService = new GrindDistributionService();
			grindDistributionForm = (GrindDistributionForm)form;
			grindDistributionViews = grindDistributionForm.getGrindDistributionViews();
			grindDistributionService.save(grindDistributionViews);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_UPDATE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch(GrindDistributionException grindDistributionException){
			throw grindDistributionException;
		}catch(Exception exception){
			GrindDistributionException grindDistributionException = null;
			grindDistributionException = new GrindDistributionException(exception, CornPPException.LAYER_ACTION, CornPPException.ACTION_LISTS);
			exception.printStackTrace();
			throw grindDistributionException;
		}
		return mapping.findForward(DEFAULT_VALUE);
	}
}